<?php
function GetUsers()
{
	//calling getConnection in global.php file to make connection to database
	$conn = getConnection();
	$userresults = $conn->query('Select * FROM Users');
	$conn->close();
	return $userresults;
}
function Productsdisplay()
{
        $conn = getConnection();
        $poutput = $conn->query("SELECT * FROM Products");
		$conn->close();
		return $poutput;
}
function Productsdisplay2($id)
{
        $conn = getConnection();
        $poutput2 = $conn->query("SELECT * FROM Products WHERE id=$id");
        $ps2 = $poutput2->fetch_assoc();
        $conn->close();
        return $ps2;}
function ordertest()
{
	$conn = getconnection();
	$ordertest = $conn->query("SELECT * FROM `order` OO Join Users UM JOIN Products PP JOIN order_detail OD ON OO.user_id=UM.id");
	return $ordertest;
}
function orderstuff()
{
        $conn = getConnection();
        $orderstuff = $conn->query("
			SELECT * FROM `order` OO
				Join order_detail OD ON OD.order_id=OO.id
				Join Products PP ON OD.product_id=PP.id
				Join Users UM ON OO.user_id=UM.id  
				");
		return $orderstuff;
}
function yourorders()
{
		$ordersyours = $_GET['orderid'];
        $conn = getConnection();
        $ordersyour = $conn->query("
			    SELECT * 
				FROM `order` OO
				Join order_detail OD ON OD.order_id=OO.id
				Join Products PP ON OD.product_id=PP.id
				Join Users UM ON OO.user_id=UM.id  
				WHERE UM.id = $ordersyours");
		$conn->close();
				if ($ordersyour == NULL)
		return "Sorry You Have no orders";
		else
		return $ordersyour;
}

function Get($id)
{
        $conn = getConnection();
        $result = $conn->query("SELECT * FROM ContactMethods CM Join Lookups L ON CM.lookup_id=L.id WHERE User_id=$id");
		return $result;
}
function Getedit($id)
{
        $conn = getConnection();
        $resultedit = $conn->query("SELECT * FROM ContactMethods CM Join Lookups L ON CM.lookup_id=L.id WHERE User_id=$id");
        $rs = $resultedit->fetch_assoc();
        $conn->close();
        return $rs;}

function GetAll()
{
	$conn = getConnection();
	//print_r($conn);
	$sql = '
	SELECT U.FirstName, U.LastName, L.Name, CM.Value
	FROM ContactMethods CM
	Join Lookups L ON CM.lookup_id = L.id
	Join Users U ON CM.User_id=U.id
	';
	return $conn->query($sql);
}
function Save($rs)
{
	if(isset($rs['id'])) // Editing
	{
		$sql = "UPDATE";
	}
	else // Adding
	{
		$sql = "INSERT INTO ContactMethods (created_at, updated_at, Lookup_id, User_id, 'Value')" . "Values ('$rs[created_at]', '$rs[updated_at]', $rs[Lookup_id], $rs[User_id], '$rs[Value]')";
	}
$result = $conn-> query($sql);
$error = $conn-> error;
$conn->close();
if($error)
	return array('Server Error' => $error);
	else
	return null;
}
function Validate($rs)
	{
		$errors = array();
		if( $rs['created_at'] == '')$errors['created_at'] = 'Create At can not be empty';
		if( $rs['Lookup_id'] == '')$errors['Lookup_id'] = 'Lookup_id is required';
		if( !is_numeric($rs['Lookup_id']))$errors['Lookup_id'] = 'Lookup_id must be a number';
		if( $rs['Lookup_id']==9 && !strpos($rs['Value'], '@') )
			$errors['Value'] = 'value must be a valid email address';
		if(count($errors) == 0)
			return null;
		return $errors;
}

?>

